/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 14-6-6
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var VERSION = '1.0',
        jQuery = $ =require('jquery-1.8.2.min');

    var defaults = {
        item:'.stickem',
        container:'.stickem-container',
        stickClass:'stickit',
        endStickClass:'stickit-end',
        offset:0,
        start:0,
        onStick:null,
        onUnstick:null
    };

    function Stickem(elem,options){
        this.$elem = $(elem);
        this.options = $.extend(true,{},defaults,options);
        this.$win = $(window);
        this.init();
    }

    Stickem.prototype = {
        init:function(){
            this.setWindowHeight();
            this.getItems();
            this.bindEvents();
        },
        bindEvents:function(){
            var that = this;
            this.$win.on('scroll.stickem',function(){
                that.handleScroll()
            });
            this.$win.on("resize.stickem",function(){
                that.handleResize();
            })
        },
        handleScroll:function(){
            if(this.items.length > 0 ){
                var pos = this.$win.scrollTop();

                for(var i = 0,len = this.items.length;i<len;i++){
                    var item = this.items[i];
                    if((item.isStuck && (pos <= item.containerStart || pos > item.scrollFinish))){
                        item.$elem.removeClass(this.options.stickClass);

                        if(pos > item.scrollFinish){
                            item.$elem.addClass(this.options.endStickClass);
                        }

                        item.isStuck = false;
                        if(this.options.onUnstick){
                            this.options.onUnstick(item);
                        }
                    }else if(item.isStuck === false && pos > item.containerStart && pos < item.scrollFinish){
                        item.$elem.removeClass(this.options.endStickClass).addClass(this.options.stickClass);

                        item.isStuck = true;
                        if(this.options.onStick){
                            this.options.onStick(item);
                        }
                    }
                }
            }
        },
        handleResize:function(){
            this.getItems();
            this.setWindowHeight();
        },
        getItem:function(index,element){
            var $this = $(element);
            var item = {
                $elem:$this,
                elemHeight:$this.height(),
                $container:$this.parents(this.options.container),
                isStuck:false
            };
            if(this.windowHeight > item.elemHeight){
                item.containerHeight = item.$container.outerHeight();
                item.containerInner = {
                    border:{
                        bottom:parseInt(item.$container.css("border-bottom"),10) || 0,
                        top:parseInt(item.$container.css("border-top"),10) || 0
                    },
                    padding:{
                        bottom:parseInt(item.$container.css('padding-bottom'),10) || 0,
                        top:parseInt(item.$container.css('padding-top'),10) || 0
                    }
                };

                item.containerInnerHeight = item.$container.height();

                item.containerStart = item.$container.offset().top - this.options.offset + this.options.start + item.containerInner.padding.top + item.containerInner.border.top;
                item.scrollFinish = item.containerStart - this.options.start + (item.containerInnerHeight - item.elemHeight);
                if(item.containerInnerHeight > item.elemHeight){
                    this.items.push(item);
                }
            }else{
                item.$elem.removeClass(this.options.stickClass + " " + this.options.endStickClass);
            }
        },
        getItems:function(){
            var that = this;
            this.items = [];
            $(this.options.item).each(function(index,elem){
                that.getItem(index,elem)
            })
        },
        setWindowHeight:function(){
            this.windowHeight = this.$win.height() - this.options.offset;
        },
        destroy:function(){
            this.$win.off("scroll.stickem");
            this.$win.off('resize.stickem');
        }
    };
    return Stickem;
});